<!DOCTYPE html>
<html>
<head>
<title>移动web开发_flex布局.md</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">

<style>
/* https://github.com/microsoft/vscode/blob/master/extensions/markdown-language-features/media/markdown.css */
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

body {
	font-family: var(--vscode-markdown-font-family, -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif);
	font-size: var(--vscode-markdown-font-size, 14px);
	padding: 0 26px;
	line-height: var(--vscode-markdown-line-height, 22px);
	word-wrap: break-word;
}

#code-csp-warning {
	position: fixed;
	top: 0;
	right: 0;
	color: white;
	margin: 16px;
	text-align: center;
	font-size: 12px;
	font-family: sans-serif;
	background-color:#444444;
	cursor: pointer;
	padding: 6px;
	box-shadow: 1px 1px 1px rgba(0,0,0,.25);
}

#code-csp-warning:hover {
	text-decoration: none;
	background-color:#007acc;
	box-shadow: 2px 2px 2px rgba(0,0,0,.25);
}

body.scrollBeyondLastLine {
	margin-bottom: calc(100vh - 22px);
}

body.showEditorSelection .code-line {
	position: relative;
}

body.showEditorSelection .code-active-line:before,
body.showEditorSelection .code-line:hover:before {
	content: "";
	display: block;
	position: absolute;
	top: 0;
	left: -12px;
	height: 100%;
}

body.showEditorSelection li.code-active-line:before,
body.showEditorSelection li.code-line:hover:before {
	left: -30px;
}

.vscode-light.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(0, 0, 0, 0.15);
}

.vscode-light.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(0, 0, 0, 0.40);
}

.vscode-light.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

.vscode-dark.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(255, 255, 255, 0.4);
}

.vscode-dark.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(255, 255, 255, 0.60);
}

.vscode-dark.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

.vscode-high-contrast.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(255, 160, 0, 0.7);
}

.vscode-high-contrast.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(255, 160, 0, 1);
}

.vscode-high-contrast.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

img {
	max-width: 100%;
	max-height: 100%;
}

a {
	text-decoration: none;
}

a:hover {
	text-decoration: underline;
}

a:focus,
input:focus,
select:focus,
textarea:focus {
	outline: 1px solid -webkit-focus-ring-color;
	outline-offset: -1px;
}

hr {
	border: 0;
	height: 2px;
	border-bottom: 2px solid;
}

h1 {
	padding-bottom: 0.3em;
	line-height: 1.2;
	border-bottom-width: 1px;
	border-bottom-style: solid;
}

h1, h2, h3 {
	font-weight: normal;
}

table {
	border-collapse: collapse;
}

table > thead > tr > th {
	text-align: left;
	border-bottom: 1px solid;
}

table > thead > tr > th,
table > thead > tr > td,
table > tbody > tr > th,
table > tbody > tr > td {
	padding: 5px 10px;
}

table > tbody > tr + tr > td {
	border-top: 1px solid;
}

blockquote {
	margin: 0 7px 0 5px;
	padding: 0 16px 0 10px;
	border-left-width: 5px;
	border-left-style: solid;
}

code {
	font-family: Menlo, Monaco, Consolas, "Droid Sans Mono", "Courier New", monospace, "Droid Sans Fallback";
	font-size: 1em;
	line-height: 1.357em;
}

body.wordWrap pre {
	white-space: pre-wrap;
}

pre:not(.hljs),
pre.hljs code > div {
	padding: 16px;
	border-radius: 3px;
	overflow: auto;
}

pre code {
	color: var(--vscode-editor-foreground);
	tab-size: 4;
}

/** Theming */

.vscode-light pre {
	background-color: rgba(220, 220, 220, 0.4);
}

.vscode-dark pre {
	background-color: rgba(10, 10, 10, 0.4);
}

.vscode-high-contrast pre {
	background-color: rgb(0, 0, 0);
}

.vscode-high-contrast h1 {
	border-color: rgb(0, 0, 0);
}

.vscode-light table > thead > tr > th {
	border-color: rgba(0, 0, 0, 0.69);
}

.vscode-dark table > thead > tr > th {
	border-color: rgba(255, 255, 255, 0.69);
}

.vscode-light h1,
.vscode-light hr,
.vscode-light table > tbody > tr + tr > td {
	border-color: rgba(0, 0, 0, 0.18);
}

.vscode-dark h1,
.vscode-dark hr,
.vscode-dark table > tbody > tr + tr > td {
	border-color: rgba(255, 255, 255, 0.18);
}

</style>

<style>
/* Tomorrow Theme */
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
/* Original theme - https://github.com/chriskempson/tomorrow-theme */

/* Tomorrow Comment */
.hljs-comment,
.hljs-quote {
	color: #8e908c;
}

/* Tomorrow Red */
.hljs-variable,
.hljs-template-variable,
.hljs-tag,
.hljs-name,
.hljs-selector-id,
.hljs-selector-class,
.hljs-regexp,
.hljs-deletion {
	color: #c82829;
}

/* Tomorrow Orange */
.hljs-number,
.hljs-built_in,
.hljs-builtin-name,
.hljs-literal,
.hljs-type,
.hljs-params,
.hljs-meta,
.hljs-link {
	color: #f5871f;
}

/* Tomorrow Yellow */
.hljs-attribute {
	color: #eab700;
}

/* Tomorrow Green */
.hljs-string,
.hljs-symbol,
.hljs-bullet,
.hljs-addition {
	color: #718c00;
}

/* Tomorrow Blue */
.hljs-title,
.hljs-section {
	color: #4271ae;
}

/* Tomorrow Purple */
.hljs-keyword,
.hljs-selector-tag {
	color: #8959a8;
}

.hljs {
	display: block;
	overflow-x: auto;
	color: #4d4d4c;
	padding: 0.5em;
}

.hljs-emphasis {
	font-style: italic;
}

.hljs-strong {
	font-weight: bold;
}
</style>

<style>
/*
 * Markdown PDF CSS
 */

 body {
	font-family: -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif, "Meiryo";
	padding: 0 12px;
}

pre {
	background-color: #f8f8f8;
	border: 1px solid #cccccc;
	border-radius: 3px;
	overflow-x: auto;
	white-space: pre-wrap;
	overflow-wrap: break-word;
}

pre:not(.hljs) {
	padding: 23px;
	line-height: 19px;
}

blockquote {
	background: rgba(127, 127, 127, 0.1);
	border-color: rgba(0, 122, 204, 0.5);
}

.emoji {
	height: 1.4em;
}

code {
	font-size: 14px;
	line-height: 19px;
}

/* for inline code */
:not(pre):not(.hljs) > code {
	color: #C9AE75; /* Change the old color so it seems less like an error */
	font-size: inherit;
}

/* Page Break : use <div class="page"/> to insert page break
-------------------------------------------------------- */
.page {
	page-break-after: always;
}

</style>

<script src="https://unpkg.com/mermaid/dist/mermaid.min.js"></script>
</head>
<body>
  <script>
    mermaid.initialize({
      startOnLoad: true,
      theme: document.body.classList.contains('vscode-dark') || document.body.classList.contains('vscode-high-contrast')
          ? 'dark'
          : 'default'
    });
  </script>
<h1 id="%E7%A7%BB%E5%8A%A8web%E5%BC%80%E5%8F%91flex%E5%B8%83%E5%B1%80">移动web开发——flex布局</h1>
<h2 id="10%E4%BC%A0%E7%BB%9F%E5%B8%83%E5%B1%80%E5%92%8Cflex%E5%B8%83%E5%B1%80%E5%AF%B9%E6%AF%94">1.0传统布局和flex布局对比</h2>
<h3 id="11%E4%BC%A0%E7%BB%9F%E5%B8%83%E5%B1%80">1.1传统布局</h3>
<ul>
<li>兼容性好</li>
<li>布局繁琐</li>
<li>局限性，不能再移动端很好的布局</li>
</ul>
<h3 id="12-flex%E5%B8%83%E5%B1%80">1.2 flex布局</h3>
<ul>
<li>操作方便，布局极其简单，移动端使用比较广泛</li>
<li>pc端浏览器支持情况比较差</li>
<li>IE11或更低版本不支持flex或仅支持部分</li>
</ul>
<h3 id="13-%E5%BB%BA%E8%AE%AE">1.3 建议</h3>
<ul>
<li>如果是pc端页面布局，还是采用传统方式</li>
<li>如果是移动端或者是不考虑兼容的pc则采用flex</li>
</ul>
<h2 id="20-flex%E5%B8%83%E5%B1%80%E5%8E%9F%E7%90%86">2.0 flex布局原理</h2>
<ul>
<li>flex 是 flexible Box 的缩写，意为&quot;弹性布局&quot;，用来为盒状模型提供最大的灵活性，任何一个容器都可以指定为 flex 布局。</li>
<li>当我们为父盒子设为 flex 布局以后，子元素的 float、clear 和 vertical-align 属性将失效。</li>
<li>flex布局又叫伸缩布局 、弹性布局 、伸缩盒布局 、弹性盒布局</li>
<li>采用 Flex 布局的元素，称为 Flex 容器（flex</li>
</ul>
<p>container），简称&quot;容器&quot;。它的所有子元素自动成为容器成员，称为 Flex 项目（flex
item），简称&quot;项目&quot;。</p>
<p><strong>总结</strong>：就是通过给父盒子添加flex属性，来控制子盒子的位置和排列方式</p>
<h2 id="30-%E7%88%B6%E9%A1%B9%E5%B8%B8%E8%A7%81%E5%B1%9E%E6%80%A7">3.0 父项常见属性</h2>
<ul>
<li>flex-direction：设置主轴的方向</li>
<li>justify-content：设置主轴上的子元素排列方式</li>
<li>flex-wrap：设置子元素是否换行</li>
<li>align-content：设置侧轴上的子元素的排列方式（多行）</li>
<li>align-items：设置侧轴上的子元素排列方式（单行）</li>
<li>flex-flow：复合属性，相当于同时设置了 flex-direction 和 flex-wrap</li>
</ul>
<h3 id="31-flex-direction%E8%AE%BE%E7%BD%AE%E4%B8%BB%E8%BD%B4%E7%9A%84%E6%96%B9%E5%90%91">3.1 flex-direction设置主轴的方向</h3>
<ul>
<li>在 flex 布局中，是分为主轴和侧轴两个方向，同样的叫法有 ： 行和列、x 轴和y 轴</li>
<li>默认主轴方向就是 x 轴方向，水平向右</li>
<li>默认侧轴方向就是 y 轴方向，水平向下</li>
</ul>
<img src="./images/1.JPG">
<ul>
<li>
<p>注意： 主轴和侧轴是会变化的，就看 flex-direction 设置谁为主轴，剩下的就是侧轴。而我们的子元素是跟着主轴来排列的</p>
<img src="./images/2.JPG">
<p>​</p>
</li>
</ul>
<h3 id="32-justify-content-%E8%AE%BE%E7%BD%AE%E4%B8%BB%E8%BD%B4%E4%B8%8A%E7%9A%84%E5%AD%90%E5%85%83%E7%B4%A0%E6%8E%92%E5%88%97%E6%96%B9%E5%BC%8F">3.2 justify-content 设置主轴上的子元素排列方式</h3>
<img src="./images/3.JPG">
<h3 id="33-flex-wrap%E8%AE%BE%E7%BD%AE%E6%98%AF%E5%90%A6%E6%8D%A2%E8%A1%8C">3.3 flex-wrap设置是否换行</h3>
<ul>
<li>默认情况下，项目都排在一条线（又称”轴线”）上。flex-wrap属性定义，flex布局中默认是不换行的。</li>
<li>nowrap 不换行</li>
<li>wrap 换行</li>
</ul>
<h3 id="34-align-items-%E8%AE%BE%E7%BD%AE%E4%BE%A7%E8%BD%B4%E4%B8%8A%E7%9A%84%E5%AD%90%E5%85%83%E7%B4%A0%E6%8E%92%E5%88%97%E6%96%B9%E5%BC%8F%E5%8D%95%E8%A1%8C">3.4 align-items 设置侧轴上的子元素排列方式（单行 ）</h3>
<ul>
<li>该属性是控制子项在侧轴（默认是y轴）上的排列方式  在子项为单项（单行）的时候使用</li>
<li>flex-start 从头部开始</li>
<li>flex-end 从尾部开始</li>
<li>center 居中显示</li>
<li>stretch 拉伸</li>
</ul>
<h3 id="35-align-content-%E8%AE%BE%E7%BD%AE%E4%BE%A7%E8%BD%B4%E4%B8%8A%E7%9A%84%E5%AD%90%E5%85%83%E7%B4%A0%E7%9A%84%E6%8E%92%E5%88%97%E6%96%B9%E5%BC%8F%E5%A4%9A%E8%A1%8C">3.5 align-content  设置侧轴上的子元素的排列方式（多行）</h3>
<p>设置子项在侧轴上的排列方式 并且只能用于子项出现 换行 的情况（多行），在单行下是没有效果的。</p>
<img src="./images/4.JPG">
<h3 id="36-align-content-%E5%92%8Calign-items%E5%8C%BA%E5%88%AB">3.6 align-content 和align-items区别</h3>
<ul>
<li>align-items  适用于单行情况下， 只有上对齐、下对齐、居中和 拉伸</li>
<li>align-content适应于换行（多行）的情况下（单行情况下无效）， 可以设置 上对齐、下对齐、居中、拉伸以及平均分配剩余空间等属性值。</li>
<li>总结就是单行找align-items  多行找 align-content</li>
</ul>
<h3 id="37-flex-flow-%E5%B1%9E%E6%80%A7%E6%98%AF-flex-direction-%E5%92%8C-flex-wrap-%E5%B1%9E%E6%80%A7%E7%9A%84%E5%A4%8D%E5%90%88%E5%B1%9E%E6%80%A7">3.7 flex-flow 属性是 flex-direction 和 flex-wrap 属性的复合属性</h3>
<pre class="hljs"><code><div>flex-flow:row wrap;
</div></code></pre>
<h2 id="40-flex%E5%B8%83%E5%B1%80%E5%AD%90%E9%A1%B9%E5%B8%B8%E8%A7%81%E5%B1%9E%E6%80%A7">4.0 flex布局子项常见属性</h2>
<ul>
<li>flex子项目占的份数</li>
<li>align-self控制子项自己在侧轴的排列方式</li>
<li>order属性定义子项的排列顺序（前后顺序）</li>
</ul>
<h3 id="41-flex-%E5%B1%9E%E6%80%A7">4.1  flex 属性</h3>
<p>flex 属性定义子项目分配剩余空间，用flex来表示占多少份数。</p>
<pre class="hljs"><code><div>.item {
    flex: &lt;number&gt;; /* 默认值 0 */
}

</div></code></pre>
<h3 id="42-align-self%E6%8E%A7%E5%88%B6%E5%AD%90%E9%A1%B9%E8%87%AA%E5%B7%B1%E5%9C%A8%E4%BE%A7%E8%BD%B4%E4%B8%8A%E7%9A%84%E6%8E%92%E5%88%97%E6%96%B9%E5%BC%8F">4.2 align-self控制子项自己在侧轴上的排列方式</h3>
<p>align-self 属性允许单个项目有与其他项目不一样的对齐方式，可覆盖 align-items 属性。</p>
<p>默认值为 auto，表示继承父元素的 align-items 属性，如果没有父元素，则等同于 stretch。</p>
<pre class="hljs"><code><div>span:nth-child(2) {
      /* 设置自己在侧轴上的排列方式 */
      align-self: flex-end;
}

</div></code></pre>
<h3 id="43-order-%E5%B1%9E%E6%80%A7%E5%AE%9A%E4%B9%89%E9%A1%B9%E7%9B%AE%E7%9A%84%E6%8E%92%E5%88%97%E9%A1%BA%E5%BA%8F">4.3 order 属性定义项目的排列顺序</h3>
<p>数值越小，排列越靠前，默认为0。</p>
<p>注意：和 z-index 不一样。</p>
<pre class="hljs"><code><div>.item {
    order: &lt;number&gt;;
}
</div></code></pre>
<h2 id="50-%E6%90%BA%E7%A8%8B%E7%BD%91%E9%A6%96%E9%A1%B5%E6%A1%88%E4%BE%8B%E5%88%B6%E4%BD%9C">5.0 携程网首页案例制作</h2>
<p>携程网链接：http://m.ctrip.com</p>
<p>1.技术选型</p>
<p>方案：我们采取单独制作移动页面方案</p>
<p>技术：布局采取flex布局</p>
<p>2.搭建相关文件夹</p>
<img src="./images/5.JPG">
<p>3.设置视口标签以及引入初始化样式</p>
<pre class="hljs"><code><div>&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, user-scalable=no,initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0&quot;&gt;

&lt;link rel=&quot;stylesheet&quot; href=&quot;css/normalize.css&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;css/index.css&quot;&gt;
</div></code></pre>
<p>4.常用初始化样式</p>
<pre class="hljs"><code><div>body {
  max-width: 540px;
  min-width: 320px;
  margin: 0 auto;
  font: normal 14px/1.5 Tahoma,&quot;Lucida Grande&quot;,Verdana,&quot;Microsoft Yahei&quot;,STXihei,hei;
  color: #000;
  background: #f2f2f2;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

</div></code></pre>
<p>5.模块名字划分</p>
<img src="./images/6.JPG">

</body>
</html>
